package cn.lovemmd.bms.controller;

import cn.lovemmd.bms.pojo.Check;
import cn.lovemmd.bms.service.CheckService;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
public class CheckController {
    private final CheckService checkService;

    @Autowired
    public CheckController(CheckService checkService) {
        this.checkService = checkService;
    }
    @RequestMapping("/list")
    public String selectCheckPage(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize) {
        JSONObject jsonObject = new JSONObject();
        IPage<Check> checkIPage = checkService.selectCheckPage(pageNum,pageSize);
        return buildJson(jsonObject,checkIPage);
    }
    @RequestMapping("/selectCheckPageByTime")
    public String selectCheckPageByTime(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize,@RequestParam("time") String time) {
        JSONObject jsonObject = new JSONObject();
        IPage<Check> checkIPage = checkService.selectCheckPageByTime(pageNum,pageSize,time);
        return buildJson(jsonObject,checkIPage);
    }
    @RequestMapping("/selectCheckPageByWay")
    public String selectCheckPageByWay(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize,@RequestParam("tWay") String tWay) {
        JSONObject jsonObject = new JSONObject();
        IPage<Check> checkIPage = checkService.selectCheckPageByWay(pageNum,pageSize,tWay);
        return buildJson(jsonObject,checkIPage);
    }
    @RequestMapping("/selectCheckPageByTimeInterval")
    public String selectCheckPageByTimeInterval(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize,@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime) {
        JSONObject jsonObject = new JSONObject();
        IPage<Check> checkIPage = checkService.selectCheckPageByTimeInterval(pageNum,pageSize,startTime,endTime);
        return buildJson(jsonObject,checkIPage);
    }
    @RequestMapping("/selectCheckPageByAmountRange")
    public String selectCheckPageByAmountRange(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize, @RequestParam("minAmount") BigDecimal minAmount, @RequestParam("maxAmount") BigDecimal maxAmount) {
        JSONObject jsonObject = new JSONObject();
        IPage<Check> checkIPage = checkService.selectCheckPageByAmountRange(pageNum,pageSize,minAmount,maxAmount);
        return buildJson(jsonObject,checkIPage);
    }
    @RequestMapping("/selectCheckPageByLessThanAmount")
    public String selectCheckPageByLessThanAmount(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize,@RequestParam("amount") BigDecimal amount) {
        JSONObject jsonObject = new JSONObject();
        IPage<Check> checkIPage = checkService.selectCheckPageByLessThanAmount(pageNum,pageSize,amount);
        return buildJson(jsonObject,checkIPage);
    }
    @RequestMapping("/selectCheckPageByGreaterThanAmount")
    public String selectCheckPageByGreaterThanAmount(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize,@RequestParam("amount") BigDecimal amount) {
        JSONObject jsonObject = new JSONObject();
        IPage<Check> checkIPage = checkService.selectCheckPageByGreaterThanAmount(pageNum,pageSize,amount);
        return buildJson(jsonObject,checkIPage);
    }
    @RequestMapping("/addCheck")
    public String addCheck(@RequestParam("tTime") String tTime,@RequestParam("tWay") String tWay,@RequestParam("tDetail") String tDetail,@RequestParam("tRemark") String tRemark,@RequestParam("tAmount") BigDecimal tAmount,@RequestParam("icbcBalance") BigDecimal icbcBalance,@RequestParam("abcBalance") BigDecimal abcBalance,@RequestParam("bcBalance") BigDecimal bcBalance,@RequestParam("wechatBalance") BigDecimal wechatBalance,@RequestParam("overallBalance") BigDecimal overallBalance) {
        JSONObject jsonObject = new JSONObject();
        Integer result = checkService.addCheck(tTime,tWay,tDetail,tRemark,tAmount,icbcBalance,abcBalance,bcBalance,wechatBalance,overallBalance);
        if(result==1){
            jsonObject.put("code",1);
            jsonObject.put("msg","添加成功");
        }else{
            jsonObject.put("code",0);
            jsonObject.put("msg","添加失败");
        }
        return jsonObject.toString();
    }
    @RequestMapping("/deleteCheck")
    public String deleteCheck(@RequestParam("tId") int tId) {
        JSONObject jsonObject = new JSONObject();
        Integer result = checkService.deleteCheck(tId);
        if(result==1){
            jsonObject.put("code",1);
            jsonObject.put("msg","删除成功");
        }else{
            jsonObject.put("code",0);
            jsonObject.put("msg","删除失败");
        }
        return jsonObject.toString();
    }
    @RequestMapping("/deleteCheckByTimeInterval")
    public String deleteCheckByTimeInterval(@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime) {
        JSONObject jsonObject = new JSONObject();
        Integer result = checkService.deleteCheckByTimeInterval(startTime,endTime);
        if(result==1){
            jsonObject.put("code",1);
            jsonObject.put("msg","删除成功");
        }else{
            jsonObject.put("code",0);
            jsonObject.put("msg","删除失败");
        }
        return jsonObject.toString();
    }
    @RequestMapping("/updateCheck")
    public String updateCheck(@RequestParam("tId") int tId,@RequestParam("tWay") String tWay,@RequestParam("tDetail") String tDetail,@RequestParam("tRemark") String tRemark,@RequestParam("tAmount") BigDecimal tAmount,@RequestParam("icbcBalance") BigDecimal icbcBalance,@RequestParam("abcBalance") BigDecimal abcBalance,@RequestParam("bcBalance") BigDecimal bcBalance,@RequestParam("wechatBalance") BigDecimal wechatBalance,@RequestParam("overallBalance") BigDecimal overallBalance) {
        JSONObject jsonObject = new JSONObject();
        Integer result = checkService.updateCheck(tId,tWay,tDetail,tRemark,tAmount,icbcBalance,abcBalance,bcBalance,wechatBalance,overallBalance);
        if(result==1){
            jsonObject.put("code",1);
            jsonObject.put("msg","修改成功");
        }else{
            jsonObject.put("code",0);
            jsonObject.put("msg","修改失败");
        }
        return jsonObject.toString();
    }
    @RequestMapping("/getTotalExpenditure")
    public String getTotalExpenditure() {
        JSONObject jsonObject = new JSONObject();
        BigDecimal totalExpenditure = checkService.getTotalExpenditure();
        jsonObject.put("code",1);
        jsonObject.put("msg","查询成功");
        jsonObject.put("data",totalExpenditure);
        return jsonObject.toString();
    }
    @RequestMapping("/getGrossIncome")
    public String getGrossIncome() {
        JSONObject jsonObject = new JSONObject();
        BigDecimal grossIncome = checkService.getGrossIncome();
        jsonObject.put("code",1);
        jsonObject.put("msg","查询成功");
        jsonObject.put("data",grossIncome);
        return jsonObject.toString();
    }

    public String buildJson(JSONObject jsonObject, IPage<Check> checkIPage){
        jsonObject.put("code",1);
        jsonObject.put("msg","查询成功");
        jsonObject.put("data",checkIPage.getRecords());
        jsonObject.put("total",checkIPage.getTotal());
        jsonObject.put("current",checkIPage.getCurrent());
        jsonObject.put("size",checkIPage.getSize());
        return jsonObject.toString();
    }
}
